package io.aggression.test.util;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class RunningConfig {

    private static int serverPort;
    private static String realmName;
    private static String logDirectory;
    private static String hostInterface;
    private static String mongoHost;
    private static String redisHost;

   //database properties
   private static String mongoOpLog;



    private static ArrayList<HandlerConfig> theHandlers = null;

    public static ArrayList<HandlerConfig> getHandlerConfigs() {
        return theHandlers;
    }

    public static int getServerPort(){
        return serverPort;
    }

    public static String  getMongoOpLog(){

    	return mongoOpLog;
    }

    public static String getRedisHost(){
    	return redisHost;
    }

    public static String getLogDirectory(){
        return logDirectory;
    }

    public static String getHostInterface(){
        return hostInterface;
    }

    public static void init(String fileName) throws XMLStreamException {

        theHandlers = new ArrayList<HandlerConfig>();

        File f = new File(fileName);
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        XMLInputFactory xmlFactory = XMLInputFactory.newFactory();
        XMLStreamReader reader = xmlFactory.createXMLStreamReader(fis);

        //maintain structure
        boolean inHandlers = false;
        boolean inDatabases = false;
        boolean inAHandler = false;


        //handler variables
        String tmpHandlerClass = null;
        String tmpHandlerContext = null;
        String tmpHandlerRole = null;

        while (reader.hasNext()) {

            int event = reader.next();

            switch (event) {
                case XMLStreamConstants.START_ELEMENT:

                    if (reader.getLocalName().equals("server-port")) {
                        serverPort = Integer.parseInt(reader.getElementText());
                    }

                    if (reader.getLocalName().equals("log-directory")) {
                        logDirectory = reader.getElementText();
                    }

                    if (reader.getLocalName().equals("host-interface")) {
                        hostInterface = reader.getElementText();
                    }

                    if (reader.getLocalName().equals("realm-name")) {
                        realmName = reader.getElementText();
                    }

                    if (reader.getLocalName().equals("handlers")) {
                        inHandlers = true;
                    }

                    if (reader.getLocalName().equals("databases")) {
                        inDatabases = true;
                    }

                    if (reader.getLocalName().equals("redis") && inDatabases) {
                        for (int i = 0; i < reader.getAttributeCount(); i++) {

                            if (reader.getAttributeLocalName(i).equals("host"))
                                redisHost = (String) reader.getAttributeValue(i);
			}
		    }

                    if (reader.getLocalName().equals("mongodb") && inDatabases) {
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            if (reader.getAttributeLocalName(i).equals("opLog"))
                                mongoOpLog = (String) reader.getAttributeValue(i);

                            if (reader.getAttributeLocalName(i).equals("host"))
                                mongoHost = (String) reader.getAttributeValue(i);
			}
		    }

                    if (reader.getLocalName().equals("handler") && inHandlers) {
                        inAHandler = true;
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            if (reader.getAttributeLocalName(i).equals("class"))
                                tmpHandlerClass = reader.getAttributeValue(i);
                            else if (reader.getAttributeLocalName(i).equals("context"))
                                tmpHandlerContext = reader.getAttributeValue(i);
                        }
                    }

                    if (reader.getLocalName().equals("security") && inAHandler)
                        tmpHandlerRole = reader.getElementText();

                    break;


                case XMLStreamConstants.END_ELEMENT:
                    if (reader.getLocalName().equals("handler")) {
                        HandlerConfig handler = new HandlerConfig(tmpHandlerClass, tmpHandlerContext, tmpHandlerRole);
                        theHandlers.add(handler);
                        inAHandler = false;
                    }

                    if (reader.getLocalName().equals("handlers")) {
                        inHandlers = false;
                    }

                    if (reader.getLocalName().equals("databases")) {
                        inDatabases = false;
                    }

                    break;
            }
            }

    }

    public static String getRealmName() {
        return realmName;
    }

    public static String getMongoHost() {
        return mongoHost;
    }

    public static class HandlerConfig {

        private String theClass = null;
        private String context = null;
        private String roleAccess = null;

        private HandlerConfig(String inClass, String inContext, String inRole) {

            theClass = inClass;
            context = inContext;
            roleAccess = inRole;
        }

        public String getClassName() {
            return theClass;
        }

        public String getContext() {
            return context;
        }

        public String getRoleAccess() {
            return roleAccess;
        }

    }
}
